/*
 * Copyright 2001-2010 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.apache.juddi.example.publish;

import org.uddi.api_v3.*;
import org.apache.juddi.ClassUtil;
import org.apache.juddi.api_v3.*;
import org.apache.juddi.v3.client.config.UDDIClientContainer;
import org.apache.juddi.v3.client.transport.Transport;
import org.uddi.v3_service.UDDISecurityPortType;
import org.uddi.v3_service.UDDIPublicationPortType;
import org.apache.juddi.v3_service.JUDDIApiPortType;

/**
 * This class takes care of publishing the file server web services
 * to a UDDI registry.
 * The UDDI registry is a Apache jUDDI registry.
 * 
 * This borrows heavily from the SimplePublish example given in the
 * user guide to jUDDI.
 */
public class PublishFS {
	private static UDDISecurityPortType security = null;
	private static JUDDIApiPortType juddiApi = null;
	private static UDDIPublicationPortType publish = null;

	/**
	 * The constructor sets up transport to the default node by
	 * using the jUDDI client API.
	 */
	public PublishFS() {
        try {
        	// Getting the default node
            String container = UDDIClientContainer.getUDDIClerkManager(null).
            	getClientConfig().getUDDINode("default").getProxyTransport();
            Class<?> transCl = ClassUtil.forName(container, Transport.class);
            
            // Setting up the transport
			if (transCl!=null) {
				Transport transport = (Transport) transCl.
					getConstructor(String.class).newInstance("default");

				security = transport.getUDDISecurityService();
				juddiApi = transport.getJUDDIApiService();
				publish = transport.getUDDIPublishService();
			}	
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}

	public void publish() {
		try {
			// First an authentication token is set up for the "root" user.
			GetAuthToken gat = new GetAuthToken();
			gat.setUserID("root");
			gat.setCred("");
			
			// Get authentification for the "root" user
			AuthToken at = security.getAuthToken(gat);

			// Then a new publisher is created
			Publisher p = new Publisher();
			p.setAuthorizedName("jasper-pub");
			p.setPublisherName("Jasper publishing");
			
			// And then the publisher is saved
			SavePublisher sp = new SavePublisher();
			sp.getPublisher().add(p);
			sp.setAuthInfo(at.getAuthInfo()); // The "root" user is used
			juddiApi.savePublisher(sp);
			
			// When the publisher is "saved", it's authentification token is retrieved
			GetAuthToken gat2 = new GetAuthToken();
			gat2.setUserID("jaspur");
			gat2.setCred("");
			AuthToken at2 = security.getAuthToken(gat2);
			
			// When this is done and we have a publisher, we are ready to create a business entity
			BusinessEntity be = new BusinessEntity();
			Name bn = new Name();
			bn.setValue("Jasper Inc.");
			be.getName().add(bn);
			
			// Using the created publisher, the business entity is "saved"
			SaveBusiness sb = new SaveBusiness();
			sb.getBusinessEntity().add(be);
			sb.setAuthInfo(at2.getAuthInfo());
			BusinessDetail bd = publish.saveBusiness(sb);
			
			// The business entity key is retrieved
			String busEnKey = bd.getBusinessEntity().get(0).getBusinessKey();
			
			// The business service is then created using the business entity key 
			BusinessService bs = new BusinessService();
			bs.setBusinessKey(busEnKey);
			Name myServName = new Name();
			myServName.setValue("Fileserver");
			bs.getName().add(myServName);
			
			// Then the bindingtemplates are created containing the EPR URLs
			// Fileserver #1
			AccessPoint apFSWS = new AccessPoint();
			apFSWS.setValue("http://localhost:8080/axis2/services/FSWS?wsdl");
			Description desfs = new Description();
			desfs.setValue("Fileserver containing database of applications.");
			BindingTemplate btFS = new BindingTemplate();
			btFS.setAccessPoint(apFSWS);
			btFS.getDescription().add(desfs);
			
			// Fileserver #2
			AccessPoint apFSdb2 = new AccessPoint();
			apFSdb2.setValue("http://localhost:8080/axis2/services/FSdb2?wsdl");
			Description desdb2 = new Description();
			desdb2.setValue("Fileserver containing database of applications.");
			BindingTemplate btFSdb2 = new BindingTemplate();
			btFSdb2.setAccessPoint(apFSdb2);
			btFSdb2.getDescription().add(desdb2);
			
			// Fileserver #3
			AccessPoint apFS2 = new AccessPoint();
			apFS2.setValue("http://192.168.0.102:8080/axis2/services/FSWS?wsdl");
			Description desfs2 = new Description();
			desfs2.setValue("Fileserver containing database of applications.");
			BindingTemplate btFS2 = new BindingTemplate();
			btFS2.setAccessPoint(apFS2);
			btFS2.getDescription().add(desfs2);
			
			// Bindingtemplates are added to collection
			BindingTemplates bt = new BindingTemplates();
			bt.getBindingTemplate().add(btFS);
			bt.getBindingTemplate().add(btFSdb2);
			bt.getBindingTemplate().add(btFS2);
			
			// And the collection is set to the business service
			bs.setBindingTemplates(bt);
			
			// "Saving" the service in the UDDI registry
			SaveService ss = new SaveService();
			ss.getBusinessService().add(bs);
			ss.setAuthInfo(at2.getAuthInfo());
			publish.saveService(ss);
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
	}		
	
	/**
	 * Main method only calls the constructor and runs the method publish
	 */
	public static void main (String args[]) {
		PublishFS pubfs = new PublishFS();
		pubfs.publish();	
	}
}
